package com.taoye.thread;

public class Demo23 {
	public static void main(String[] args) throws InterruptedException {
		Demo23Service service = new Demo23Service();
		Thread t = new Demo23ThreadA(service);
		t.setName("A");
		t.start();
		Thread t2 = new Demo23ThreadB(service);
		t2.setName("B");
		t2.start();
	}
}

class Demo23Service {
	synchronized public void fun1() {
		System.out.println(Thread.currentThread().getName()+"线程执行了fun1");
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("fun1结束");
	}
	
	synchronized public void fun2() {
		System.out.println(Thread.currentThread().getName()+"线程执行了fun2");
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("fun2结束");
	}
}

class Demo23ThreadA extends Thread {
	private Demo23Service service;
	
	public Demo23ThreadA(Demo23Service service) {
		this.service = service;
	}
	@Override
	public void run() {
		service.fun1();
	}
}

class Demo23ThreadB extends Thread {
	private Demo23Service service;
	
	public Demo23ThreadB(Demo23Service service) {
		this.service = service;
	}
	@Override
	public void run() {
		service.fun2();
	}
}
